//
//  AppDelegate.swift
//  微博
//
//  Created by lipengfei on 15/9/2.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = defaultRootViewController()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "jumpToController:", name: "WBJumpToController", object: nil)
        window?.makeKeyAndVisible()
        
        return true
    }
    ///  启动默认根控制器
    private func defaultRootViewController() -> UIViewController{
        //首先判断用户是否登录
        if WBUserAccountViewModel.sharedUserAccount.userLogin {
            return isNewVersion() ? WBNewFeatureViewController() : WBWelcomeViewController()
        }
        return WBMainTabBarController()
    }
    
    ////    检测程序的当前版本
    private func isNewVersion() ->Bool{
//        print( NSBundle.mainBundle().infoDictionary)
        //1. 获取当前程序版本
        let version = Double( NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
        // 2, 获取存储的版本
        // 版本号保存在沙盒中，从沙盒中获取版本号
        let versionKey = "WBVersion"
        // 如果沙盒中没有版本号，返回0
         let saxboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        // 3.  保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(version!, forKey: versionKey)
        // 4. 比较 俩版本，返回结果
        return version > saxboxVersion
    }
    
    func jumpToController(notification:NSNotification){
        
        window?.rootViewController  = (notification.object == nil) ? WBMainTabBarController() : WBWelcomeViewController()
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

